package com.remenote.remenoteback.controller;

import com.remenote.remenoteback.entity.req.UserRequest;
import com.remenote.remenoteback.entity.resp.ServerResponse;
import com.remenote.remenoteback.service.UserService;
import com.remenote.remenoteback.entity.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/login")
    public ServerResponse<UserVo> login(@RequestBody UserRequest request){
        return userService.login(request.getUsername(), request.getPassword());
    }
    @PostMapping("/validate")
    public ServerResponse validate(){
        // Validated by interceptor
        return ServerResponse.buildSuccessResponse();
    }

    @PostMapping("/register")
    public ServerResponse<UserVo> register(@RequestBody UserRequest request){
        return userService.register(request.getUsername(), request.getPassword());
    }
    @PostMapping("/changeNick")
    public ServerResponse changeNickname(@RequestBody UserRequest request){
        log.error("change nickname");
        return userService.changeNickname(request.getNewNickname());
    }
    @PostMapping("/changePass")
    public ServerResponse changePassword(@RequestBody UserRequest request){
        return userService.changePassword(request.getPassword(), request.getNewPassword());
    }

    @PostMapping("/logout")
    public ServerResponse logout(){
        return userService.logout();
    }
    @PostMapping("/joingroup")
    public ServerResponse joinGroup(@RequestBody UserRequest request){
        return userService.joinStudyGroup(request.getGroupName());
    }
}